package com.wugui.datax.rpc.serialize.impl;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.wugui.datax.rpc.serialize.Serializer;
import com.wugui.datax.rpc.util.XxlRpcException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * hessian serialize
 *
 * @author xuxueli 2015-9-26 02:53:29
 */
public class HessianSerializer extends Serializer {

  @Override
  public <T> byte[] serialize(T obj) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Hessian2Output ho = new Hessian2Output(os);
    try {
      ho.writeObject(obj);
      ho.flush();
      return os.toByteArray();
    } catch (IOException e) {
      throw new XxlRpcException(e);
    } finally {
      try {
        ho.close();
      } catch (IOException ignored) {
      }
      try {
        os.close();
      } catch (IOException ignored) {
      }
    }
  }

  @Override
  public <T> Object deserialize(byte[] bytes, Class<T> clazz) {
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    Hessian2Input hi = new Hessian2Input(is);
    try {
      return hi.readObject();
    } catch (IOException e) {
      throw new XxlRpcException(e);
    } finally {
      try {
        hi.close();
      } catch (Exception ignored) {
      }
      try {
        is.close();
      } catch (IOException ignored) {
      }
    }
  }
}
